package com.lanou.sfu.songforu.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.activity.MainActivity;
import com.lanou.sfu.songforu.datas.MusicListDatas;
import com.lanou.sfu.songforu.datas.SearchSingleSong;
import com.lanou.sfu.songforu.utils.cache.MyConfig;


import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by quyan on 15/10/29.
 */
public class SearchSingSongAdapter extends BaseExpandableListAdapter {
    //Group的内容(父容器)
    private List<SearchSingleSong.DataEntity> datas;
    private Context context;
    private String[] quailty;


    public SearchSingSongAdapter(Context context, List<SearchSingleSong.DataEntity> dataEntities) {
        this.context = context;
        this.datas = dataEntities;
        notifyDataSetChanged();
    }


    @Override
    public int getGroupCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;//列表数
    }

    @Override
    public Object getGroup(int groupPosition) {
        return datas != null && datas.size() > 0 ? datas.get(groupPosition) : null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup holderGroup = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.searchsinglesong_item, null);
            holderGroup = new ViewHolderGroup();
            holderGroup.songTitle = (TextView) convertView.findViewById(R.id.song_title);
            holderGroup.songFavourNum = (TextView) convertView.findViewById(R.id.song_fivort_num);
            holderGroup.songListTime = (TextView) convertView.findViewById(R.id.song_list_time);
            holderGroup.heart = (ImageView) convertView.findViewById(R.id.heart);
            holderGroup.songSinger = (TextView) convertView.findViewById(R.id.song_singer);
            convertView.setTag(holderGroup);
        } else {
            holderGroup = (ViewHolderGroup) convertView.getTag();
        }
        //获取数据
        SearchSingleSong.DataEntity dataEntity = (SearchSingleSong.DataEntity) getGroup(groupPosition);
        holderGroup.songListTime.setText(groupPosition + 1 + "");
        holderGroup.songTitle.setText(dataEntity.getName());//歌名
        holderGroup.songFavourNum.setText(dataEntity.getFavorites());//喜爱人数
        holderGroup.songSinger.setText(dataEntity.getSingerName());//歌手
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
        ViewHolderChild holderChild = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.searchsingsong_childitem, null);
            holderChild = new ViewHolderChild();
            holderChild.musicCollect = (TextView) convertView.findViewById(R.id.music_collection);
            holderChild.musicDownLoad = (TextView) convertView.findViewById(R.id.music_download);
            holderChild.musicshare = (TextView) convertView.findViewById(R.id.music_enjoy);
            holderChild.musicPlay = (TextView) convertView.findViewById(R.id.music_play);
            //播放
            holderChild.musicPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArrayList<MusicListDatas> list = new ArrayList<>();
                    if (datas.get(groupPosition).getAuditionList().size() > 0) {
                        for (SearchSingleSong.DataEntity entity : datas) {
                            MusicListDatas listDatas = new MusicListDatas();
                            if (entity.getAuditionList().size() > 0) {
                                listDatas.setName(entity.getName());
                                listDatas.setSinger(entity.getSingerName());
                                Log.i("xxxx", entity.getName());
                                Log.i("xxxx", entity.getAuditionList().get(0).getUrl());
                                listDatas.setSongId(entity.getAuditionList().get(0).getUrl());
                                listDatas.setFrom("搜索");
                                list.add(listDatas);
                            }
                        }
                    } else {
                        Toast.makeText(context, "由于版权方禁止,此服务臣妾办不到啊o(︶︿︶)o", Toast.LENGTH_SHORT).show();
                    }
                    MainActivity.play(list, groupPosition);
                }
            });
            //收藏监听
            holderChild.musicCollect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            //下载监听
            holderChild.musicDownLoad.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DownloadDialog(groupPosition);

                }
            });

            //分享监听
            holderChild.musicshare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public class ViewHolderGroup {
        private TextView songListTime;
        private TextView songTitle;
        private TextView songSinger;
        private ImageView heart;
        private TextView songFavourNum;
    }

    public class ViewHolderChild {
        private TextView musicDownLoad;
        private TextView musicshare;
        private TextView musicPlay;
        private TextView musicCollect;
    }

    /**
     * 点击下载弹出对话框
     */
    private void DownloadDialog(final int position) {
        final List<SearchSingleSong.DataEntity.AuditionListEntity> entity = datas.get(position).getAuditionList();
        //数组初始化时一定要定义它的长度
        quailty = new String[entity.size()];
        //获取小数点后两位的方法
        DecimalFormat df = new DecimalFormat("0.00");
        for (int i = 0; i < entity.size(); i++) {
            String str = entity.get(i).getTypeDescription();
            double size = entity.get(i).getSize();
            Log.i("tttttt", str);

            quailty[i] = str + df.format(size / 1024000) + "M";
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("请选择下载品质");
        builder.setItems(quailty, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context, entity.get(which).getTypeDescription() + "已加入到下载列表中", Toast.LENGTH_SHORT).show();
                String url = entity.get(which).getUrl();
                String singerName = datas.get(position).getSingerName();
                String title = datas.get(position).getName();

                MyConfig.putSharePre(context, "url", "url", url);
                MyConfig.putSharePre(context, "singerName", "singerName", singerName);
                MyConfig.putSharePre(context, "title", "name", title);
                Log.i("ttttttt", url + "8888888");
                Log.i("ttttttt", title + "888888888888");
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).show();
    }
}
